﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;

namespace prjColecao
{
    public partial class deletarItem : System.Web.UI.Page
    {
        static string connectionString = "Data Source=teiom;User ID=COLECAO;Password=OU812;Unicode=True";
        protected OracleConnection sConn = new OracleConnection("Data Source=teiom;User ID=COLECAO;Password=OU812;Unicode=True");
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public void getConnection()
        {
            dataConnect();
        }

        protected void grdItemDeletar_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                getConnection();
            } 
            catch (Exception EConnection)
            {
                throw new Exception(EConnection.Message);
            }

            int index_item = grdItemDeletar.SelectedIndex;
            // string id_item = grdItemDeletar.HeaderRow.Cells[index_item].Text;
            string id_item = grdItemDeletar.Rows[index_item].Cells[1].Text;


            char isOk = 'N';
            try
            {
                //sConn.Open(); // to verify
                OracleCommand oraCmd = new OracleCommand();
                oraCmd.Connection = sConn;
                oraCmd.CommandText = "COLECAO.PKG_ITENS_COLECAO.PROC_DEL_ITEM_COLECAO";
                oraCmd.CommandType = CommandType.StoredProcedure;

                oraCmd.Parameters.AddWithValue("P_ID_ITEM", id_item);
                oraCmd.ExecuteNonQuery();

                isOk = 'Y';
                
            }
            catch (OracleException ErrOra)
            {
                throw new Exception(ErrOra.Message);

            }
            finally
            {
                dataDisconnect();
            }

            if (isOk == 'Y')
            {
                Response.Redirect("SucessoOperacao.aspx");
                
            }
            else{
                Response.Redirect("ErroOperacao.aspx");
            }
        }

        #region connect to database
        public void dataConnect()
        {
            try
            {
                if (sConn.State == 0)
                {
                    sConn.Open();
                    Console.WriteLine("Conexao OK");
                }

            }
            catch (Exception NoConnect)
            {
                Console.WriteLine("Conexao NAO OK");
                throw new Exception("There is no Connection" + NoConnect.Message);
            }
        }
        #endregion

        #region disconnect from database
        public void dataDisconnect()
        {
            try
            {
                sConn.Close();
            }
            catch (Exception NoConnect)
            {
                throw new Exception("No response:" + NoConnect.Message);
            }
        }

        #endregion







    }
}